"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
Object.defineProperty(exports, "__esModule", { value: true });
var core_1 = require("@angular/core");
var role_1 = require("./role");
var RoleComponent = /** @class */ (function () {
    function RoleComponent(router, roleService) {
        this.router = router;
        this.roleService = roleService;
        this.currentPage = 1;
        this.itemsPerPage = 12;
        this.isShow = false;
        this.roleInfo = new role_1.RoleInfo();
        this.roleList = new role_1.RoleList();
        this.addRole = new role_1.AddRole();
        this.editRole = new role_1.EditRole();
        this.permissionGroup = [];
        this.permissionCheck = [];
        this.permissionList = [];
        this.name = '';
        this.belong = '';
        this.orderField = '';
        this.orderSort = '';
    }
    RoleComponent.prototype.ngOnInit = function () {
        this.getRoleList();
    };
    /*获取角色列表*/
    RoleComponent.prototype.getRoleList = function () {
        var _this = this;
        this.roleService.getRoleList(this.name, this.belong, this.orderField, this.orderSort, this.currentPage, this.itemsPerPage).subscribe(function (data) {
            if (data.code === 2000) {
                _this.roleList.list = data.data.data;
                _this.currentPage = data.data.currentPage;
                _this.totalItems = data.data.total;
            }
        });
    };
    /*角色详情*/
    RoleComponent.prototype.getRoleInfo = function (id) {
        var _this = this;
        this.roleService.getRoleInfo(id).subscribe(function (data) {
            if (data.code === 2000) {
                _this.roleInfo = data.data;
            }
        });
    };
    /*角色添加*/
    RoleComponent.prototype.addRoleSubmit = function () {
        var _this = this;
        this.roleService.addRole(this.addRole).subscribe(function (data) {
            if (data.code === 2000) {
                _this.type = 'success';
                _this.message = '添加成功';
                _this.isShow = true;
                _this.addModal.hide();
                _this.getRoleList();
            }
        });
    };
    /*角色修改*/
    RoleComponent.prototype.editRoleSubmit = function () {
        var _this = this;
        this.editRole.id = this.roleInfo.id;
        this.editRole.name = this.roleInfo.name;
        this.editRole.slug = this.roleInfo.slug;
        this.editRole.description = this.roleInfo.description;
        this.editRole.level = this.roleInfo.level;
        this.editRole.belong = this.roleInfo.belong;
        this.roleService.editRole(this.editRole).subscribe(function (data) {
            if (data.code === 2000) {
                _this.type = 'success';
                _this.message = '修改成功';
                _this.isShow = true;
                _this.editModal.hide();
                _this.getRoleList();
            }
        });
    };
    RoleComponent.prototype.getPermissionGroup = function (id) {
        var _this = this;
        this.setId = id;
        this.roleService.getPermissionGroup().subscribe(function (data) {
            if (data.code === 2000) {
                _this.permissionGroup = data.data;
                console.log(_this.permissionGroup);
                _this.giveRolePermission(id);
            }
        });
    };
    RoleComponent.prototype.giveRolePermission = function (id) {
        var _this = this;
        this.roleService.getRolePermission(id).subscribe(function (data) {
            _this.permissionCheck = data.data;
            _this.permissionGroup.forEach(function (value, index, array) {
                value.data.forEach(function (value2, index2, array2) {
                    value2.checked = true;
                    if (data.data.indexOf(value2.id) === -1) {
                        value2.checked = false;
                    }
                });
            });
        });
    };
    RoleComponent.prototype.rolePermissionEdit = function () {
        var _this = this;
        var permission = JSON.stringify(this.permissionCheck);
        this.roleService.setRolePermission(permission, this.setId).subscribe(function (data) {
            if (data === 2000) {
                _this.type = 'success';
                _this.message = '修改成功';
                _this.isShow = true;
            }
        });
    };
    RoleComponent.prototype.checkRolePermission = function (e, id) {
        if (e.target.checked) {
            this.permissionCheck.push(id);
        }
        else {
            for (var i = 0; i < this.permissionCheck.length; i++) {
                if (this.permissionCheck[i] === id) {
                    this.permissionCheck.splice(i, 1);
                    break;
                }
            }
        }
    };
    ;
    RoleComponent.prototype.pageChanged = function (event) {
        window.scrollTo(0, 0);
        this.currentPage = event.page;
        this.getRoleList();
    };
    RoleComponent.prototype.promptIsShow = function (event) {
        this.isShow = event;
    };
    __decorate([
        core_1.ViewChild('addModal')
    ], RoleComponent.prototype, "addModal", void 0);
    __decorate([
        core_1.ViewChild('editModal')
    ], RoleComponent.prototype, "editModal", void 0);
    RoleComponent = __decorate([
        core_1.Component({
            selector: 'app-role',
            templateUrl: './role.component.html',
            styleUrls: ['./role.component.scss']
        })
    ], RoleComponent);
    return RoleComponent;
}());
exports.RoleComponent = RoleComponent;
